D:\git\skunkworks\herald-for-cpp\herald-tests\bledatabase-tests.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2020-2021 Herald Project Contributors |
2 | | // SPDX-License-Identifier: Apache-2.0 |
3 | | // |
4 | | |
5 | | #include <memory> |
6 | | #include <vector> |
7 | | |
8 | | #include "test-templates.h" |
9 | | |
10 | | #include "catch.hpp" |
11 | | |
12 | | #include "herald/herald.h" |
13 | | |
14 | 1 | TEST_CASE("ble-database-empty", "[ble][database][ctor][empty]") { |
15 | 1 | SECTION("ble-database-empty") { |
16 | 1 | DummyLoggingSink dls; |
17 | 1 | DummyBluetoothStateManager dbsm; |
18 | 1 | herald::DefaultPlatformType dpt; |
19 | 1 | herald::Context ctx(dpt,dls,dbsm); // default context include |
20 | 1 | using CT = typename herald::Context<herald::DefaultPlatformType,DummyLoggingSink,DummyBluetoothStateManager>; |
21 | 1 | herald::ble::ConcreteBLEDatabase<CT> db(ctx); |
22 | 1 | |
23 | 1 | REQUIRE(db.size() == 0); |
24 | 1 | } |
25 | 1 | } |
26 | | |
27 | 1 | TEST_CASE("ble-database-callback-verify", "[ble][database][callback][verify]") { |
28 | 1 | SECTION("ble-callback-verify") { |
29 | 1 | DummyLoggingSink dls; |
30 | 1 | DummyBluetoothStateManager dbsm; |
31 | 1 | herald::DefaultPlatformType dpt; |
32 | 1 | herald::Context ctx(dpt,dls,dbsm); // default context include |
33 | 1 | using CT = typename herald::Context<herald::DefaultPlatformType,DummyLoggingSink,DummyBluetoothStateManager>; |
34 | 1 | herald::ble::ConcreteBLEDatabase<CT> db(ctx); |
35 | 1 | DummyBLEDBDelegate delegate; |
36 | 1 | db.add(delegate); |
37 | 1 | |
38 | 1 | REQUIRE(db.size() == 0); |
39 | 1 | REQUIRE(delegate.createCallbackCalled == false); |
40 | 1 | REQUIRE(delegate.updateCallbackCalled == false); |
41 | 1 | REQUIRE(delegate.deleteCallbackCalled == false); |
42 | 1 | |
43 | 1 | herald::datatype::Data devMac(std::byte(0x02),6); |
44 | 1 | herald::datatype::TargetIdentifier dev(devMac); |
45 | 1 | |
46 | 1 | // add in new device |
47 | 1 | herald::ble::BLEDevice& devPtr = db.device(dev); |
48 | 1 | REQUIRE(db.size() == 1); |
49 | 1 | REQUIRE(delegate.createCallbackCalled == true); |
50 | 1 | REQUIRE(delegate.updateCallbackCalled == false); |
51 | 1 | REQUIRE(delegate.deleteCallbackCalled == false); |
52 | 1 | REQUIRE(delegate.dev.has_value()); |
53 | 1 | REQUIRE(delegate.dev.value().get() == devPtr); |
54 | 1 | |
55 | 1 | // add in a second device via the payload, not target identifier |
56 | 1 | herald::datatype::PayloadData payload(std::byte(0x1f),6); |
57 | 1 | herald::ble::BLEDevice& devPtr2 = db.device(payload); |
58 | 1 | REQUIRE(db.size() == 2); |
59 | 1 | REQUIRE(delegate.createCallbackCalled == true); |
60 | 1 | REQUIRE(delegate.updateCallbackCalled == true); // true because payload set on create |
61 | 1 | REQUIRE(delegate.deleteCallbackCalled == false); |
62 | 1 | REQUIRE(delegate.dev.has_value()); |
63 | 1 | REQUIRE(delegate.dev.value().get() == devPtr2); |
64 | 1 | |
65 | 1 | // update a device attribute |
66 | 1 | devPtr.rssi(herald::datatype::RSSI{14}); |
67 | 1 | REQUIRE(db.size() == 2); |
68 | 1 | REQUIRE(delegate.createCallbackCalled == true); |
69 | 1 | REQUIRE(delegate.updateCallbackCalled == true); |
70 | 1 | REQUIRE(delegate.deleteCallbackCalled == false); |
71 | 1 | REQUIRE(delegate.dev.has_value()); |
72 | 1 | REQUIRE(delegate.dev.value().get() == devPtr); |
73 | 1 | |
74 | 1 | // delete the device |
75 | 1 | db.remove(dev); |
76 | 1 | REQUIRE(db.size() == 1); |
77 | 1 | REQUIRE(delegate.createCallbackCalled == true); |
78 | 1 | REQUIRE(delegate.updateCallbackCalled == true); |
79 | 1 | REQUIRE(delegate.deleteCallbackCalled == true); |
80 | 1 | REQUIRE(delegate.dev.has_value()); |
81 | 1 | REQUIRE(delegate.dev.value().get() == devPtr); |
82 | 1 | |
83 | 1 | // delete non existant |
84 | 1 | |
85 | 1 | herald::datatype::Data devMac3(std::byte(0x1d),6); |
86 | 1 | herald::datatype::TargetIdentifier dev3(devMac3); |
87 | 1 | |
88 | 1 | // add in new device |
89 | 1 | db.remove(dev3); |
90 | 1 | // nothing should have changed |
91 | 1 | REQUIRE(db.size() == 1); |
92 | 1 | REQUIRE(delegate.createCallbackCalled == true); |
93 | 1 | REQUIRE(delegate.updateCallbackCalled == true); |
94 | 1 | REQUIRE(delegate.deleteCallbackCalled == true); |
95 | 1 | REQUIRE(delegate.dev.has_value()); |
96 | 1 | REQUIRE(delegate.dev.value().get() == devPtr); |
97 | 1 | |
98 | 1 | } |
99 | 1 | } |
100 | | |
101 | | |
102 | | |
103 | 1 | TEST_CASE("ble-database-macrotate-samepayload", "[ble][database][macrotate][samepayload]") { |
104 | 1 | SECTION("ble-callback-macrotate-samepayload") { |
105 | 1 | DummyLoggingSink dls; |
106 | 1 | DummyBluetoothStateManager dbsm; |
107 | 1 | herald::DefaultPlatformType dpt; |
108 | 1 | herald::Context ctx(dpt,dls,dbsm); // default context include |
109 | 1 | using CT = typename herald::Context<herald::DefaultPlatformType,DummyLoggingSink,DummyBluetoothStateManager>; |
110 | 1 | herald::ble::ConcreteBLEDatabase<CT> db(ctx); |
111 | 1 | DummyBLEDBDelegate delegate; |
112 | 1 | db.add(delegate); |
113 | 1 | |
114 | 1 | REQUIRE(db.size() == 0); |
115 | 1 | REQUIRE(delegate.createCallbackCalled == false); |
116 | 1 | REQUIRE(delegate.updateCallbackCalled == false); |
117 | 1 | REQUIRE(delegate.deleteCallbackCalled == false); |
118 | 1 | |
119 | 1 | herald::datatype::Data devMac(std::byte(0x02),6); |
120 | 1 | herald::datatype::TargetIdentifier dev(devMac); |
121 | 1 | |
122 | 1 | // add in new device |
123 | 1 | herald::ble::BLEDevice& devPtr = db.device(dev); |
124 | 1 | auto devices = db.matches([](auto& deviceRef) { |
125 | 1 | return true; |
126 | 1 | }); |
127 | 1 | std::cout << "Devices:-" << std::endl; |
128 | 1 | for (auto& d : devices) { |
129 | 1 | std::cout << "Mac: " << d.get().identifier() |
130 | 1 | << ", State: " << ((d.get().state() == herald::ble::BLEDeviceState::uninitialised) ? "uninitialised"0 : "initialised") |
131 | 1 | << ", Payload: " << (d.get().payloadData().size() > 0 ? d.get().payloadData().hexEncodedString()0 : "Empty") << std::endl; |
132 | 1 | } |
133 | 1 | REQUIRE(db.size() == 1); |
134 | 1 | REQUIRE(delegate.createCallbackCalled == true); |
135 | 1 | REQUIRE(delegate.updateCallbackCalled == false); |
136 | 1 | REQUIRE(delegate.deleteCallbackCalled == false); |
137 | 1 | REQUIRE(delegate.dev.has_value()); |
138 | 1 | REQUIRE(delegate.dev.value().get() == devPtr); |
139 | 1 | |
140 | 1 | // read its payload |
141 | 1 | herald::data::PayloadData devPayload(std::byte(0x55),24); |
142 | 1 | devPtr.payloadData(devPayload); |
143 | 1 | devices = db.matches([](auto& deviceRef) { |
144 | 1 | return true; |
145 | 1 | }); |
146 | 1 | std::cout << "Devices:-" << std::endl; |
147 | 1 | for (auto& d : devices) { |
148 | 1 | std::cout << "Mac: " << d.get().identifier() |
149 | 1 | << ", State: " << ((d.get().state() == herald::ble::BLEDeviceState::uninitialised) ? "uninitialised"0 : "initialised") |
150 | 1 | << ", Payload: " << (d.get().payloadData().size() > 0 ? d.get().payloadData().hexEncodedString() : "Empty"0 ) << std::endl; |
151 | 1 | } |
152 | 1 | REQUIRE(db.size() == 1); |
153 | 1 | REQUIRE(delegate.createCallbackCalled == true); |
154 | 1 | REQUIRE(delegate.updateCallbackCalled == true); |
155 | 1 | REQUIRE(delegate.deleteCallbackCalled == false); |
156 | 1 | REQUIRE(delegate.dev.has_value()); |
157 | 1 | REQUIRE(delegate.dev.value().get() == devPtr); |
158 | 1 | |
159 | 1 | // add a second device via mac |
160 | 1 | herald::datatype::Data dev2Mac(std::byte(0x03),6); |
161 | 1 | herald::datatype::TargetIdentifier dev2(dev2Mac); |
162 | 1 | herald::ble::BLEDevice& dev2Ptr = db.device(dev2); |
163 | 2 | devices = db.matches([](auto& deviceRef) { |
164 | 2 | return true; |
165 | 2 | }); |
166 | 1 | std::cout << "Devices:-" << std::endl; |
167 | 2 | for (auto& d : devices) { |
168 | 2 | std::cout << "Mac: " << d.get().identifier() |
169 | 2 | << ", State: " << ((d.get().state() == herald::ble::BLEDeviceState::uninitialised) ? "uninitialised"0 : "initialised") |
170 | 2 | << ", Payload: " << (d.get().payloadData().size() > 0 ? d.get().payloadData().hexEncodedString()1 : "Empty"1 ) << std::endl; |
171 | 2 | } |
172 | 1 | REQUIRE(db.size() == 2); |
173 | 1 | REQUIRE(delegate.createCallbackCalled == true); |
174 | 1 | REQUIRE(delegate.updateCallbackCalled == true); // from first device's payload |
175 | 1 | REQUIRE(delegate.deleteCallbackCalled == false); |
176 | 1 | REQUIRE(delegate.dev.has_value()); |
177 | 1 | REQUIRE(delegate.dev.value().get() == dev2Ptr); |
178 | 1 | |
179 | 1 | // now assume the first device's mac rotates |
180 | 1 | herald::datatype::Data devRefreshMac(std::byte(0x08),6); |
181 | 1 | herald::datatype::TargetIdentifier devRefresh(devRefreshMac); |
182 | 1 | herald::ble::BLEDevice& devRefreshPtr = db.device(devRefresh); |
183 | 3 | devices = db.matches([](auto& deviceRef) { |
184 | 3 | return true; |
185 | 3 | }); |
186 | 1 | std::cout << "Devices:-" << std::endl; |
187 | 3 | for (auto& d : devices) { |
188 | 3 | std::cout << "Mac: " << d.get().identifier() |
189 | 3 | << ", State: " << ((d.get().state() == herald::ble::BLEDeviceState::uninitialised) ? "uninitialised"0 : "initialised") |
190 | 3 | << ", Payload: " << (d.get().payloadData().size() > 0 ? d.get().payloadData().hexEncodedString()1 : "Empty"2 ) << std::endl; |
191 | 3 | } |
192 | 1 | REQUIRE(db.size() == 3); |
193 | 1 | REQUIRE(delegate.createCallbackCalled == true); |
194 | 1 | REQUIRE(delegate.updateCallbackCalled == true); |
195 | 1 | REQUIRE(delegate.deleteCallbackCalled == false); |
196 | 1 | REQUIRE(delegate.dev.has_value()); |
197 | 1 | REQUIRE(delegate.dev.value().get() == devRefreshPtr); |
198 | 1 | |
199 | 1 | // now fetch its payload - should reduce the total number of devices |
200 | 1 | herald::data::PayloadData devRefreshPayload(std::byte(0x55),24); // same as before |
201 | 1 | devRefreshPtr.payloadData(devRefreshPayload); |
202 | 2 | devices = db.matches([](auto& deviceRef) { |
203 | 2 | return true; |
204 | 2 | }); |
205 | 1 | std::cout << "Devices:-" << std::endl; |
206 | 2 | for (auto& d : devices) { |
207 | 2 | std::cout << "Mac: " << d.get().identifier() |
208 | 2 | << ", State: " << ((d.get().state() == herald::ble::BLEDeviceState::uninitialised) ? "uninitialised"0 : "initialised") |
209 | 2 | << ", Payload: " << (d.get().payloadData().size() > 0 ? d.get().payloadData().hexEncodedString()1 : "Empty"1 ) << std::endl; |
210 | 2 | } |
211 | 1 | REQUIRE(db.size() == 2); // original targetID with this payload should have been deleted |
212 | 1 | REQUIRE(delegate.createCallbackCalled == true); |
213 | 1 | REQUIRE(delegate.updateCallbackCalled == true); |
214 | 1 | REQUIRE(delegate.deleteCallbackCalled == true); |
215 | 1 | REQUIRE(delegate.dev.has_value()); |
216 | 1 | REQUIRE(delegate.dev.value().get() == devRefreshPtr); |
217 | 1 | } |
218 | 1 | } |